package com.lmk.ms.tp.service;

import com.lmk.ms.common.cache.GlobalCacheService;
import com.lmk.ms.common.constants.QqApi;
import com.lmk.ms.common.dto.tp.qq.QqAccessToken;
import com.lmk.ms.common.dto.tp.qq.QqOpenId;
import com.lmk.ms.common.dto.tp.qq.QqUserInfo;
import com.lmk.ms.tp.config.QqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

/**
 * QQ API接口服务
 *
 * @author laomake@hotmail.com
 * @version 1.0
 * @date 2022/02/27
 */
@Slf4j
public class QqApiService {

    @Autowired
    QqProperties qqProperties;

    @Autowired
    GlobalCacheService globalCacheService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 获取QQ授权页面地址
     *
     * @param state
     * @return
     */
    public String getAuthorizeUrl(String state) {
        StringBuilder sb = new StringBuilder();
        sb.append(QqApi.AUTHORIZE)
                .append("?response_type=code")
                .append("&scope=get_user_info")
                .append("&client_id=").append(qqProperties.getAppId())
                .append("&redirect_uri=").append(qqProperties.getRedirectUrl())
                .append("&state=").append(state);

        return sb.toString();
    }

    /**
     * 获取访问令牌
     *
     * @return
     */
    public QqAccessToken getAccessToken(String code) {
        StringBuilder sb = new StringBuilder();
        sb.append(QqApi.ACCESS_TOKEN)
                .append("?grant_type=authorization_code")
                .append("&client_id=").append(qqProperties.getAppId())
                .append("&client_secret=").append(qqProperties.getAppKey())
                .append("&code=").append(code)
                .append("&redirect_uri=").append(qqProperties.getRedirectUrl())
                .append("&fmt=json");

        QqAccessToken response = restTemplate.getForObject(sb.toString(), QqAccessToken.class);
        if (response.getCode() != null) {
            log.error("QQ获取访问令牌失败：{}", response.getMsg());
            response = null;
        }
        return response;
    }

    /**
     * 获取用户OpenId
     *
     * @param accessToken
     * @return
     */
    public QqOpenId getOpenId(String accessToken) {
        StringBuilder sb = new StringBuilder();
        sb.append(QqApi.OPEN_ID)
                .append("?access_token=").append(accessToken)
                .append("&fmt=json");

        QqOpenId response = restTemplate.getForObject(sb.toString(), QqOpenId.class);
        if (response.getCode() != null) {
            log.error("QQ获取OpenId失败：{}", response.getMsg());
        }
        return response;
    }

    /**
     * 获取用户信息
     *
     * @param accessToken
     * @return
     */
    public QqUserInfo getUserInfo(String accessToken) {
        StringBuilder sb = new StringBuilder();
        sb.append(QqApi.USER_INFO)
                .append("?access_token=").append(accessToken)
                .append("&oauth_consumer_key=").append(qqProperties.getAppId())
                .append("&fmt=json");

        QqUserInfo response = restTemplate.getForObject(sb.toString(), QqUserInfo.class);
        if (response.getRet() != 0) {
            log.error("QQ获取用户信息失败：{}", response.getMsg());
        }
        return response;
    }

}
